<?php
/**
 * Frontpage Model for JA News Component
 * 
 * @package    Joomla
 * @subpackage Components
 * @license		GNU/GPL
 * @author JoomlArt.com
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );
require_once (JPATH_SITE . '/components/com_content/helpers/route.php');

class JanewsModelJanews extends JModel
{
	/**
	 * @return Info Frontpage
	 */
	function getFrontpageInfo()
	{
		$db = &JFactory::getDBO();
		$query = 'SELECT * FROM #__janews_frontpage'
		.' WHERE default_f = 1';
		$db->setQuery($query);
		$rs = $db->loadObject();
		return $rs;
	}
	
	/**
	 * @var id of the template
	 * @return info of the template
	 */
	function getTemplInfo($id)
	{
		$db = &JFactory::getDBO();
		$query = 'SELECT * FROM #__janews_templates'
		.' WHERE id = '.$id;
		$db->setQuery($query);
		$rs = $db->loadObject();
		return $rs;
	}
	
	function getSectionInfo($id)
	{
		$db = &JFactory::getDBO();
		$query = 'SELECT * FROM #__janews_sections'
		.' WHERE secid = '.$id;
		$db->setQuery($query);
		$rs = $db->loadObject();
		return $rs;
	}
	
	//get a record from sections table
	function getSection($id)
	{
	  $db = &JFactory::getDBO();
	  $query = 'SELECT * FROM #__sections'
    				 .' WHERE id='.$id;
		$db->setQuery($query);
		$section = $db->loadObject();
		return $section;
	}
	
	//get the information for the current category
	function getCategory($id)
	{
	  $db = &JFactory::getDBO();
		$query = 'SELECT c.*, s.id as sectionid, s.title as sectiontitle,' .
			' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as slug'.
			' FROM #__categories AS c' .
			' INNER JOIN #__sections AS s ON s.id = c.section' .
			' WHERE c.id = '. (int) $id;
		$db->setQuery($query);
		$db->query();
		$category = $db->loadObject();
		return $category;
	}
	
	
	/**
	 * @param ID of the section
	 * @return string selected cat_id of the section 
	 */
	function getStrCatIds($s_id)
	{
		if ($s_id) {
			$db = JFactory::getDBO();
			$query = 'SELECT categories FROM #__janews_sections WHERE secid ='.$s_id;
			$db->setQuery($query);
			$rs = $db->loadResult();
		}
		return $rs;
	}
	
	function getListNoGroup($show_fp, $secid, $hlids, $start=0, $limit =4)
	{
	  global $mainframe;

		$db 	=& JFactory::getDBO();
		$user 	=& JFactory::getUser();
		$aid	= $user->get('aid', 0);

		$contentConfig	= &JComponentHelper::getParams( 'com_content' );
		$noauth			= !$contentConfig->get('shownoauth');

		jimport('joomla.utilities.date');
		$date = new JDate();
		$now = $date->toMySQL();

		$nullDate = $db->getNullDate();
		
		$query = 'SELECT a.*,'
		   .' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
		   .' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'
		   .' CHAR_LENGTH( a.`fulltext` ) AS readmore,'
		   .' u.name AS author, u.usertype, cc.title AS category, g.name AS groups'
		   .' FROM #__content AS a'
		   .' INNER JOIN #__categories AS cc ON cc.id = a.catid'
		   .' LEFT JOIN #__sections AS s ON s.id = a.sectionid'
		   .' LEFT JOIN #__users AS u ON u.id = a.created_by'
		   .' LEFT JOIN #__groups AS g ON a.access = g.id'
		   .' WHERE a.state = 1'
		   .($noauth ? ' AND a.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid : '')
		   .($show_fp ? ' AND s.id IN ('.  $secid .')': ' AND cc.id IN ('. $secid.')' )
		   .($hlids ? ' AND a.id NOT IN ('.$hlids.')' : '')
		   .' AND s.published = 1 AND cc.published = 1'
		   .' AND ( publish_up = '.$db->Quote($nullDate).' OR publish_up <= '.$db->Quote($now).' )'
		   .' AND ( publish_down = '.$db->Quote($nullDate).' OR publish_down >= '.$db->Quote($now).' )'
		   .' ORDER BY  a.created DESC'
		   ." LIMIT $start, $limit";
		$db->setQuery($query);
		$rows = $db->loadObjectList();
		for($i=0;$i<count($rows);$i++) {
		  $rows[$i]->text = $rows[$i]->introtext;
    }
		return $rows;
	}
	
	function getTotalListNoGroup($show_fp, $secid, $hlids)
	{
	  global $mainframe;

		$db 	=& JFactory::getDBO();
		$user 	=& JFactory::getUser();
		$aid	= $user->get('aid', 0);

		$contentConfig	= &JComponentHelper::getParams( 'com_content' );
		$noauth			= !$contentConfig->get('shownoauth');

		jimport('joomla.utilities.date');
		$date = new JDate();
		$now = $date->toMySQL();

		$nullDate = $db->getNullDate();
		
		$query = 'SELECT count(*)'
		   .' FROM #__content AS a'
		   .' INNER JOIN #__categories AS cc ON cc.id = a.catid'
		   .' LEFT JOIN #__sections AS s ON s.id = a.sectionid'
		   .' LEFT JOIN #__users AS u ON u.id = a.created_by'
		   .' LEFT JOIN #__groups AS g ON a.access = g.id'
		   .' WHERE a.state = 1'
		   .($noauth ? ' AND a.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid : '')
		   .($show_fp ? ' AND s.id IN ('.  $secid .')': ' AND cc.id IN ('. $secid.')' )
		   .($hlids ? ' AND a.id NOT IN ('.$hlids.')' : '')
		   .' AND s.published = 1 AND cc.published = 1'
		   .' AND ( publish_up = '.$db->Quote($nullDate).' OR publish_up <= '.$db->Quote($now).' )'
		   .' AND ( publish_down = '.$db->Quote($nullDate).' OR publish_down >= '.$db->Quote($now).' )';
		$db->setQuery($query);
		$num = $db->loadResult();
		return $num;
	}
	
	function getBlogContentGroup($show_fp, $secids,$items,$hlids,$order)
	{
		$contents = array();
		//query to determine article of the blog frontpage
		for ($i = 0; $i < count($secids); $i ++) {
		  $secid = $secids[$i];
		  if($secid) {
	  	  $rows = $this->getList($show_fp, $secid, $items, $hlids,$order);
	  	  if(count($rows)) {
	        $contents[] = $rows;
	        //$themes[] = '';
	      }
	    }
	  } 
    return $contents;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $show_fp
	 * @param unknown_type $catid
	 * @param unknown_type $items
	 * @param unknown_type $hlids
	 * @return object list
	 */
	
	function getList($show_fp, $catid, $items, $hlids, $orderby= 'created desc')
	{
		global $mainframe;

		$db 	=& JFactory::getDBO();
		$user 	=& JFactory::getUser();
		$aid	= $user->get('aid', 0);

		$contentConfig	= &JComponentHelper::getParams( 'com_content' );
		$noauth			= !$contentConfig->get('shownoauth');

		jimport('joomla.utilities.date');
		$date = new JDate();
		$now = $date->toMySQL();

		$nullDate = $db->getNullDate();

		// query to determine article count
		$query = 'SELECT a.*,cc.description as catdesc, cc.title as cattitle,s.description as secdesc, s.title as sectitle,'
			.' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
			.' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'
			.' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as secslug'
			.' FROM #__content AS a'
			.' INNER JOIN #__categories AS cc ON cc.id = a.catid'
			.' INNER JOIN #__sections AS s ON s.id = a.sectionid'
			.' WHERE a.state = 1 '
			.($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '')
			.' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' ) '
			.' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
			.(($show_fp)? ' AND s.id=' . (int) $catid : "\n AND cc.id=" . (int) $catid)
			.($hlids ? ' AND a.id NOT IN ('.$hlids.')' : '')
			.' AND cc.section = s.id'
			.' AND cc.published = 1'
			.' AND s.published = 1'
			.' ORDER BY '.$orderby;
		$db->setQuery($query, 0, $items);
		$rows = $db->loadObjectList();
		global $mainframe;
  		JPluginHelper::importPlugin('content');
  		$dispatcher	   =& JDispatcher::getInstance();
  		$params 	   =& $mainframe->getParams('com_content');
  		$limitstart	= JRequest::getVar('limitstart', 0, '', 'int');
  
  		for($i=0;$i<count($rows);$i++) {
  		  $rows[$i]->text = $rows[$i]->introtext;
  		  $results = $dispatcher->trigger('onPrepareContent', array (& $rows[$i], & $params, $limitstart));
   		  $rows[$i]->introtext = $rows[$i]->text;
      }
		return $rows;
	}
		
	function buildQueryHLNews($items, $show_fp, $strcats=false,$listofhlcontent=false, $hlarticles_id=false) {
	  $db 	=& JFactory::getDBO();
	  $contentConfig	= &JComponentHelper::getParams( 'com_content' );
		$noauth			= !$contentConfig->get('shownoauth');
		$user		=& JFactory::getUser();
		$aid	= $user->get('aid', 0);

		jimport('joomla.utilities.date');
		$date = new JDate();
		$now = $date->toMySQL();

		$nullDate = $db->getNullDate();

    //show frontpage item normal
		if (!$show_fp&&$listofhlcontent==0){
		$query = "SELECT a.*,"
				. "\n CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(':', a.id, a.alias) ELSE a.id END as slug,"
				. "\n CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(':', cc.id, cc.alias) ELSE cc.id END as catslug"
				. "\n FROM #__content AS a"
				. "\n INNER JOIN #__content_frontpage AS f ON f.content_id = a.id"
				. "\n INNER JOIN #__categories AS cc ON cc.id = a.catid"
				. "\n WHERE ( a.state = 1 AND a.sectionid > 0 "
				. ' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' ) ' 
				. ' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )' 
				. ($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid : '')
				. "\n AND cc.published = 1 )"
				. "\n ORDER BY f.ordering ASC"
				. "\n LIMIT ".$items;
		}
		//list of headline content for section headline
		else {
		  $query = "SELECT a.*,"
				. "\n CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(':', a.id, a.alias) ELSE a.id END as slug,"
				. "\n CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(':', cc.id, cc.alias) ELSE cc.id END as catslug"
				. "\n FROM #__content AS a";
			
				switch ($listofhlcontent) {
				  //list headline front page
				  case 0:
				    $query .= "\n INNER JOIN #__content_frontpage AS f ON f.content_id = a.id"
  					. "\n INNER JOIN #__categories AS cc ON cc.id = a.catid"
  					. "\n WHERE ( a.state = 1 AND a.sectionid > 0 "
  					. ' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' ) ' 
  					. ' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )' 
  					. ($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid : '')
  					. ($strcats ? ' AND a.catid IN (' .$strcats.')' : '')
  					. "\n AND cc.published = 1 )"
  					. "\n ORDER BY f.ordering ASC"
  					. "\n LIMIT ".$items;
  					break;
  				//list headline lastest
				  case 1:
				    $query .= "\n INNER JOIN #__categories AS cc ON cc.id = a.catid"
  					. "\n WHERE ( a.state = 1 AND a.sectionid > 0 "
  					. ' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' ) ' 
  					. ' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )' 
  					. ($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid : '')
  					. ($strcats ? ' AND a.catid IN (' .$strcats.')' : '')
  					. "\n AND cc.published = 1 )"
  					. "\n ORDER BY a.created DESC"
  					. "\n LIMIT ".$items;
  					break;
					//list alow itemid
				  case 2:
				    $query .= "\n INNER JOIN #__categories AS cc ON cc.id = a.catid"
  					. "\n WHERE ( a.state = 1 AND a.sectionid > 0 "
  					. ' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' ) ' 
  					. ' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )' 
  					. ($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid : '')
  					. ' AND a.id IN (' .$hlarticles_id.')'
  					. "\n AND cc.published = 1 )"
  					. "\n ORDER BY a.ordering ASC"
  					. "\n LIMIT ".$items;
  					break;
				}
		}
		return $query;
	}

	function getHLNews($items, $show_fp, $strcats=false,$listofhlcontent=false,$hlarticles_id=false) {
		$db 	=& JFactory::getDBO();
	  $query = $this->buildQueryHLNews($items, $show_fp, $strcats,(int)$listofhlcontent, $hlarticles_id);
		$db->setQuery($query);
		$news = $db->loadObjectList();

		global $mainframe;
  		JPluginHelper::importPlugin('content');
  		$dispatcher	   =& JDispatcher::getInstance();
  		$params 	   =& $mainframe->getParams('com_content');
  		$limitstart	= JRequest::getVar('limitstart', 0, '', 'int');
  
  		for($i=0;$i<count($news);$i++) {
   		  $news[$i]->text = $news[$i]->introtext;
 		    $results = $dispatcher->trigger('onPrepareContent', array (& $news[$i], & $params, $limitstart));
   		  $news[$i]->introtext = $news[$i]->text;
      }
		return $news;
	}

	function getAllCatIds ($catorsec) {
		$db 	=& JFactory::getDBO();
		if($catorsec){
			$query = "SELECT id FROM #__categories WHERE published = 1;";
		}
		else{
			$query = "SELECT id FROM #__sections WHERE published = 1;";
		}
		$db->setQuery($query);
		$c = $db->loadObjectList();
		$catids = array();
		foreach($c as $_c)
		  $catids[] = $_c->id;
		  
		return $catids;
	}

	function getAllCatIdsBySection ($sectionid) {
		$db 	=& JFactory::getDBO();
		$query = "SELECT id FROM #__categories WHERE section = $sectionid;";

		$db->setQuery($query);
		$c = $db->loadObjectList();
		$catids = array();
		foreach($c as $_c)
		  $catids[] = $_c->id;
		  
		return $catids;
	}
	
	function getComponentParameters($comid) {
		$db 	=& JFactory::getDBO();
		$query = "SELECT params FROM #__modules WHERE id = $comid;";
		$db->setQuery($query);
		return $db->loadResult();
  }

	function getItemid($sectionid) {
		$db 	=& JFactory::getDBO();
		$query = "SELECT id FROM #__menu WHERE `link` like '%option=com_content%view=section%id=$sectionid%'";
		$db->setQuery($query);
		echo $db->getQuery();
		return $db->loadResult();
  }
  
  
  function orderbyPrimary($orderby)
	{
		switch ($orderby)
		{
			case 'alpha' :
				$orderby = 'cc.title, ';
				break;
			case 'ralpha' :
				$orderby = 'cc.title DESC, ';
				break;
			case 'order' :
				$orderby = 'cc.ordering, ';
				break;
			default :
				$orderby = '';
				break;
		}
		return $orderby;
	}
	
	function orderbySecondary($orderby)
	{
		switch ($orderby)
		{
			case 'date' :
				$orderby = 'a.created';
				break;
			case 'rdate' :
				$orderby = 'a.created DESC';
				break;
			case 'alpha' :
				$orderby = 'a.title';
				break;
			case 'ralpha' :
				$orderby = 'a.title DESC';
				break;
			case 'hits' :
				$orderby = 'a.hits DESC';
				break;
			case 'rhits' :
				$orderby = 'a.hits';
				break;
			case 'order' :
				$orderby = 'a.ordering';
				break;
			case 'author' :
				$orderby = 'a.created_by_alias, u.name';
				break;
			case 'rauthor' :
				$orderby = 'a.created_by_alias DESC, u.name DESC';
				break;
			case 'front' :
				$orderby = 'f.ordering';
				break;
			default :
				$orderby = 'a.ordering';
				break;
		}
		return $orderby;
	}
	
  /**
   * Return itemid of headline
   */
  function getHeadlineIds($items,$show_fp, $strcats=false, $listofhlcontent=false, $hlarticles_id=false)
  {
    $query = $this->buildQueryHLNews($items, $show_fp, $strcats, $listofhlcontent, $hlarticles_id);
    $db 	=& JFactory::getDBO();
		$db->setQuery($query);
		$news = $db->loadObjectList();
		$ids = ''; $c = count($news);
		if ($c > 0){
  		for ($i=0; $i<$c-1; $i++) {
  		  $ids .= $news[$i]->id.',';
  		}
  		$ids .= $news[$c-1]->id;
		}
		return $ids;
  }
    
  function getMenuCategoryParams($c_id)
  {
    $db = JFactory::getDBO();
	  $component =& JComponentHelper::getComponent('com_janews');
    $query = "SELECT params"
      ." FROM #__menu"
      ." WHERE menutype='janews' AND componentid = ".$component->id." AND link='index.php?option=com_janews&view=category&id=".$c_id."'";
    $db->setQuery($query);
    $catparams = $db->loadResult();
    $file 	= JPATH_COMPONENT_ADMINISTRATOR.DS.'views'.DS.'sectionslayout'.DS.'catparams.xml';
    $catparams = new JParameter($catparams, $file);
    return $catparams;
  }
  
  function buildQueryCatHLNews($items,$catid,$catparams)
  {
    $db = JFactory::getDBO();
    $contentConfig	= &JComponentHelper::getParams( 'com_content' );
		$noauth			= !$contentConfig->get('shownoauth');
		$user		=& JFactory::getUser();
		$aid	= $user->get('aid', 0);

		jimport('joomla.utilities.date');
		$date = new JDate();
		$now = $date->toMySQL();

		$nullDate = $db->getNullDate();

    $orderby_sec	= $catparams->get('orderby_pri','rdate');
		$orderby_sec	= ($orderby_sec == 'front') ? '' : $orderby_sec;
		$orderby_pri	= $catparams->get('orderby_pri','');
		$secondary		= $this->orderbySecondary($orderby_sec).', ';
		$primary		  = $this->orderbyPrimary($orderby_pri);
    $orderby      = $primary .' '. $secondary .' a.created DESC';

		$query = 'SELECT cc.title AS category, a.id, a.title, a.title_alias, a.introtext, a.fulltext, a.sectionid, a.state, a.catid, a.created, a.created_by, a.created_by_alias, a.modified, a.modified_by,' .
			' a.checked_out, a.checked_out_time, a.publish_up, a.publish_down, a.attribs, a.hits, a.images, a.urls, a.ordering, a.metakey, a.metadesc, a.access,' .
			' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
			' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'.
			' CHAR_LENGTH( a.`fulltext` ) AS readmore, u.name AS author, u.usertype' .
			' FROM #__content AS a' .
			' LEFT JOIN #__categories AS cc ON a.catid = cc.id' .
			' LEFT JOIN #__users AS u ON u.id = a.created_by' .
			' WHERE ( a.state = 1 AND a.sectionid > 0 '.
			' AND catid = '.$catid.
			' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' ) ' .
			' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )' .
			($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid : '') .' )' .
			' ORDER BY '.$orderby .
		  ' LIMIT '.$items;
	  return $query;
  }
   
  function getCatHLNews($items,$catid,$catparams)
  {
    $db 	=& JFactory::getDBO();
	  $query = $this->buildQueryCatHLNews($items, $catid, $catparams);
		$db->setQuery($query);
		$news = $db->loadObjectList();

		global $mainframe;
		JPluginHelper::importPlugin('content');
		$dispatcher	   =& JDispatcher::getInstance();
		$params 	   =& $mainframe->getParams('com_content');
		$limitstart	= JRequest::getVar('limitstart', 0, '', 'int');

		for($i=0;$i<count($news);$i++) {
 		  $news[$i]->text = $news[$i]->introtext;
	    $results = $dispatcher->trigger('onPrepareContent', array (& $news[$i], & $params, $limitstart));
 		  $news[$i]->introtext = $news[$i]->text;
    }
		return $news;
  }
   
  function getCatHeadlineIds($items,$catid,$catparams)
  {
    $query = $this->buildQueryCatHLNews($items,$catid,$catparams);
    $db 	=& JFactory::getDBO();
		$db->setQuery($query);
		$news = $db->loadObjectList();
		$ids = ''; $c = count($news);
		if ($c > 0){
  		for ($i=0; $i<$c-1; $i++) {
  		  $ids .= $news[$i]->id.',';
  		}
  		$ids .= $news[$c-1]->id;
		}
		return $ids;
  }
  
  function getCategoryContent($catid, $catparams)
  {
    global $mainframe;

		$db 	=& JFactory::getDBO();
		$user 	=& JFactory::getUser();
		$aid	= $user->get('aid', 0);

		$contentConfig	= &JComponentHelper::getParams( 'com_content' );
		$noauth			= !$contentConfig->get('shownoauth');

		jimport('joomla.utilities.date');
		$date = new JDate();
		$now = $date->toMySQL();

		$nullDate = $db->getNullDate();
		
	  $orderby_sec	= $catparams->get('orderby_pri','rdate');
		$orderby_sec	= ($orderby_sec == 'front') ? '' : $orderby_sec;
		$orderby_pri	= $catparams->get('orderby_pri','');
		$secondary		= $this->orderbySecondary($orderby_sec).', ';
		$primary		  = $this->orderbyPrimary($orderby_pri);
    $orderby      = $primary .' '. $secondary .' a.created DESC';
		
		$query = 'SELECT a.*,'
		   .' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
		   .' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'
		   .' CHAR_LENGTH( a.`fulltext` ) AS readmore,'
		   .' u.name AS author, u.usertype, cc.title AS category, g.name AS groups'
		   .' FROM #__content AS a'
		   .' INNER JOIN #__categories AS cc ON cc.id = a.catid'
		   .' LEFT JOIN #__users AS u ON u.id = a.created_by'
		   .' LEFT JOIN #__groups AS g ON a.access = g.id'
		   .' WHERE a.state = 1'
		   .($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid : '')
		   .' AND cc.id = '.$catid
		   //.($hlids ? ' AND a.id NOT IN ('.$hlids.')' : '')
		   .' AND cc.published = 1'
		   .' AND ( publish_up = '.$db->Quote($nullDate).' OR publish_up <= '.$db->Quote($now).' )'
		   .' AND ( publish_down = '.$db->Quote($nullDate).' OR publish_down >= '.$db->Quote($now).' )'
		   .' ORDER BY '.$orderby;
		$db->setQuery($query);
		$rows = $db->loadObjectList();
		for($i=0;$i<count($rows);$i++) {
		  $rows[$i]->text = $rows[$i]->introtext;
    }
		return $rows;
  }
}
